#include <SDL.h>
#include <fstream>
#include <math.h>
extern SDL_Surface* screen;
#include "Enemy.h"
#include "Zombie.h"

SDL_Surface* Zombie::sprite1 =0;
SDL_Surface* Zombie::sprite2 =0;
SDL_Surface* Zombie::mugshot =0;

    Zombie::Zombie(float x, float y){
        type = ZOMBIE;
        this->x = x;
        this->y = y;
        prevx = x;
        prevy = y;
        vx = 0;
        vy = 0;
        stuntime = 0;
        animationtimer = 0;
        state = APPROACHING;
        currentanimation = sprite1;
        height = sprite1->h;
        width = sprite1->w;
        score = WEREWOLF_SCORE;
    }

    void Zombie::Update(Uint32 elapsedtime, double angle, bool lightstate, int mousex, int mousey)
    {
        UpdateState(elapsedtime, angle, lightstate, mousex, mousey);
        UpdateSpeed();
        UpdatePosition(elapsedtime);
        Draw(elapsedtime);
    }

    void Zombie::UpdateState(Uint32 elapsedtime,double angle, bool lightstate, int mousex, int mousey)
    {
        switch(state)
        {
        case APPROACHING:
            if(lightstate && LightContact(angle, mousex,mousey))
                state = STUNNED;
            break;
        case STUNNED:
            stuntime += elapsedtime;
            if(stuntime >= WEREWOLF_STUN_TIME)
                state = SCARED;
            break;
        case SCARED:
            break;
        default:
            break;
        }
    }

    //TODO: implement
    bool Zombie::LightContact(double angle, int mx, int my)
    {
        double myangle;
        int degreerange =10;
        //if close enough
        if(sqrt((x+sprite1->w/2 - screen->w/2)*(x+sprite1->w/2 - screen->w/2) + ((screen->h-100) - (y+sprite1->h/2))*((screen->h-100) - (y+sprite1->h/2))) <= 200)
        {
            if(abs(x+sprite1->w/2 - screen->w/2) < 3) myangle = M_PI_2;
            else if(mx > screen->w/2)
                myangle = atan(((screen->h - 100) - (y + sprite1->h/2))/(x+sprite1->w/2 - screen->w/2));
            else if(mx < screen->w/2)
                myangle = atan(((screen->h - 100) - (y + sprite1->h/2))/-(x+sprite1->w/2 - screen->w/2));
            if(myangle>M_PI)
                myangle = M_PI;
            else if(myangle < 0)
                myangle = 0;
            myangle = myangle/M_PI * 180;
            if(x+ sprite1->w/2 <= screen->w/2 && mx <= screen->w/2)
            {
                if(myangle-degreerange <= angle && angle <= myangle+degreerange)
                    return true;
            }
            else if(x+sprite1->w/2 > screen->w/2 && mx > screen->w/2)
            {
                if(myangle-degreerange <= angle && angle <= myangle+degreerange)
                    return true;
            }
        }
        return false;
    }

    void Zombie::UpdateSpeed()
    {
        switch(state)
        {
            case APPROACHING:
            if(x + sprite1->w/2 < screen->w/2-10)
                vx = WEREWOLF_SPEED;
            else if(x + sprite1->w/2 > screen->w/2+10)
                vx = -WEREWOLF_SPEED;
            else vx = 0;
            if(y < screen->h-100-64)
                vy = WEREWOLF_SPEED;
            else vy=0;
                break;
            case STUNNED:
                vx = vy = 0;
                break;
            case SCARED:
            if(x + sprite1->w/2 < screen->w/2)
                vx = -WEREWOLF_ESCAPE_SPEED;
            else if(x + sprite1->w/2 > screen->w/2)
                vx = WEREWOLF_ESCAPE_SPEED;
            if(x<-100 || x>screen->w+100)
                deleteflag = true;
                break;
            default:
                break;
        }
    }

    void Zombie::UpdatePosition(Uint32 elapsedtime){
        x += (float)vx * elapsedtime/1000;
        y += (float)vy * elapsedtime/1000;
    }

    void Zombie::Draw(Uint32 elapsedtime)
    {
        animationtimer+=elapsedtime;
        if(animationtimer>500)
        {
            if(currentanimation==sprite1)
                currentanimation=sprite2;
            else
                currentanimation=sprite1;
            animationtimer=0;
        }
        SDL_Rect drawcoord;
        drawcoord.x = x;    drawcoord.y = y;
        SDL_BlitSurface(currentanimation,NULL,screen,&drawcoord);
    }

